<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */

namespace MoChat\App\Client\Action\Dashboard;

use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Client\Model\Client;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;

/**
 * .
 * @Controller
 */
class Binding extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     *
     * @RequestMapping(path="/dashboard/client/binding", methods="POST")
     */
    public function handle(): array
    {
        $user = user();
        if (empty($user)) {
            throw new CommonException(ErrorCode::AUTH_UNAUTHORIZED, '登入异常请重新登入');
        }
        $data = $this->request->all();
        $this->validated($data);
        if ($data['employee_id']==0){
            Client::whereIn('id', $data['id'])->update([
                'b2b_manager_name' => null,
                'work_employee_id' => 0,
                'allocate_time'=> date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        }else{
            $workEmployee = Db::table('work_employee')->where('id', $data['employee_id'])->select('name','id')->first();
            if (!empty($workEmployee)){
                $data['id'] = is_array($data['id']) ? $data['id'] : [$data['id']];
                if (isset($data['type']) && $data['type']==2){
                    Client::whereIn('id', $data['id'])->update([
                        'b2b_salesman_name' => $workEmployee->name ?? '',
                        'salesman_id' =>$workEmployee->id ?? 0,
                        'updated_at' => date('Y-m-d H:i:s'),
                    ]);
                }else{
                    Client::whereIn('id', $data['id'])->update([
                        'b2b_manager_name' => $workEmployee->name ?? '',
                        'work_employee_id' =>$workEmployee->id ?? 0,
                        'allocate_time'=> date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ]);
                }


            }
        }

        return [];
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'id' => 'required',
            'employee_id' => 'required|numeric',
            'type'=>'in:1,2'
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'id.required' => '会员id未填写',
            'employee_id.required' => '员工id未填写',
            'employee_id.numeric' => '员工id参数错误',
            'type.in'=>'分配类型参数错误'
        ];
    }
}
